//
//  ViewController.m
//  15--UICollectionView_CycleLife
//
//  Created by 冀永金 on 16/8/3.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
#define ANYCOLOR  [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1.0]
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((self.view.bounds.size.width - 30) / 2, (self.view.bounds.size.width - 30) / 4);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    UICollectionView *collection = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:collection];
    collection.delegate = self;
    collection.dataSource = self;
    collection.backgroundColor = ANYCOLOR;
    [collection registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"pool"];
    
    // 关于collectionView LifeCycle
    // 1.numberOfSectionsInCollectionView
    // 2.collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    // 3---5循环执行
    //3. prepareForResuse (从重用池取出时调用)
    //4.(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    // 5.collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
    // 当cell完全出屏幕时
    //6.collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
    // iOS 10以前
    // 出屏幕立即返回，依旧进入3-->4-->5
    // iOS10以后
    // 出屏幕立即返回 依旧执行 4-->5 不会走3  提高了效率
}
#pragma mark -----CollectionView LifeCycle
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
      NSLog(@"---[%@:%s]\n\n\n\n",self,__func__);
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
     NSLog(@"---[%@:%s]\n\n\n\n",self,__func__);
    return 29;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    //cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"报纸"]];
    cell.backgroundColor = ANYCOLOR;
    return cell;
}
#pragma mark ----iOS 8.0以后推出，cell将要显示在屏幕时
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@-----%ld---[%@:%s]\n\n\n\n",cell,indexPath.row,self,__func__);
}
#pragma mark------cell将要消失在屏幕时
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@-----%ld---[%@:%s]\n\n\n\n",cell,indexPath.row,self,__func__);

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
